/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: packet_manager.h
 *
 * Purpose: packet manager implementation
 *
 * Developer:
 *   wen.gu , 2020-9-02
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __PANDA_PKT_PACKET_MANAGER_H__
#define __PANDA_PKT_PACKET_MANAGER_H__

#include <string>
#include <memory>
#include <vector>

#include "panda/core/types.h"
#include "panda/core/errors.h"


/******************************************************************************
 **    MACROS
 ******************************************************************************/
#ifdef __cplusplus
#define PKT_EXTERN_C extern "C"
#else
#define PKT_EXTERN_C 
#endif


#if defined(_MSC_VER) /** !!!for visual studio */
 ////////////////////////////////////////////////////////////////////////////
 /** compat the keyword 'inline' */
#if (_MSC_VER > 1200) && !defined (__cplusplus)
#define inline    _inline  /** to compat keyword 'inline' */
#endif

#if defined (PKT_DLL_EXPORT)
#define PKT_API_EXPORT __declspec(dllexport)
#else
#define PKT_API_EXPORT

#endif
#else /** for other toolchain */
#define PKT_API_EXPORT 
#endif

 /** general api decorate macro */
#define PKT_API PKT_API_EXPORT /**c++ api */

#define PKT_API_C PKT_EXTERN_C PKT_API_EXPORT /** c api */

/** general class decorate macro*/
#define PKT_CLASS PKT_API_EXPORT

/** general global variable decorate macro(for external reference ) */
#define PKT_VAR  extern PKT_API_EXPORT 

/** general const global variable decorate macro(for external reference ) */
#define PKT_CVAR PKT_VAR const 



 // Conditional NORETURN attribute on the throw functions would:
 // a) suppress false positives from static code analysis
 // b) possibly improve optimization opportunities.
#if !defined(PKT_NO_RETURN)
#if defined(_MSC_VER)
#define PKT_NO_RETURN __declspec(noreturn)
#elif defined(__GNUC__)
#define PKT_NO_RETURN __attribute__((__noreturn__))
#else
#define PKT_NO_RETURN
#endif
#endif
/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
namespace panda
{
namespace pkt
{

enum class PacketType: uint8_t
{
    Any = 0,  /** not care about the type or indicat do operation for all types */
    Application = 1,
    Library = 2,
    Service = 3,
    Resource = 4
};

struct PacketInfo
{   
    PacketType type;
    std::string name;
    std::string version;
    std::string osVersion; /** depend on PandaOS version */
};

struct PacketItem
{
    PacketType type;
    std::string name;
};

using PacketList = std::vector<PacketItem>;
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class PKT_CLASS PacketManager final
{
public:
    using PandaErrc = panda::core::PandaErrc;

public:
    ~PacketManager();
    static PacketManager& get();
public:
    /**name: a packet file with full path */
    PandaErrc install(const std::string& name, PacketType type, bool force = false);

    /**name: a packet file with full path */
    PandaErrc update(const std::string& name, PacketType type);

    /**name: packet name only, do not include path */
    PandaErrc remove(const std::string& name, PacketType type);

    /**name: packet name only, do not include path */
    PandaErrc queryInfo(const std::string& name, PacketType type, PacketInfo& info);

    /**name: packet name only, do not include path */
    PandaErrc queryList(const std::string& name, PacketType type, PacketList& pl);
protected:
    PacketManager();

private:
    class impl;
    std::unique_ptr<impl> mImpl;
};


} /** namespace pkt*/
} /** namespace panda*/

#endif /** !__PANDA_PKT_PACKET_MANAGER_H__ */